package com.yenlien.traceyun.breed.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.yenlien.traceyun.breed.dao.BreedDao;
import com.yenlien.traceyun.breed.entity.Breed;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.yenlien.traceyun.breed.dao.BreedDetailDao;
import com.yenlien.traceyun.breed.entity.BreedDetail;

@Service
public class BreedDetailService {
    @Autowired
    private BreedDetailDao breedDetailDao;

    @Autowired
    private BreedDao breedDao;

    public Page<BreedDetail> findByList(final BreedDetail breedDetail) {
        Pageable pageable = PageRequest.of(breedDetail.getPage(), breedDetail.getPageSize(), Sort.Direction.ASC, ("time"));
        return breedDetailDao.findAll(new Specification<BreedDetail>() {
            private static final long serialVersionUID = -1396496630802017658L;

            @Override
            public Predicate toPredicate(Root<BreedDetail> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> source = root.get("source");
                Path<String> code = root.get("code");
                Path<String> equipmentName = root.get("equipmentName");
                Path<String> relevanceId = root.get("relevanceId");
                //构造查询条件
                List<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmpty(breedDetail.getSource())) {
                    list.add(cb.equal(source, breedDetail.getSource()));
                }
                if (!StringUtils.isEmpty(breedDetail.getRelevanceId())) {
                    list.add(cb.equal(relevanceId, breedDetail.getRelevanceId()));
                }
                Predicate[] predicateA = new Predicate[list.size()];
                Predicate predicateAnd = cb.and(list.toArray(predicateA));

                if (StringUtils.isNotEmpty(breedDetail.getCode())) {
                    List<Predicate> list2 = new ArrayList<>();
                    list2.add(cb.like(code, "%" + breedDetail.getCode() + "%"));
                    list2.add(cb.like(equipmentName, "%" + breedDetail.getEquipmentName() + "%"));
                    Predicate[] predicateO = new Predicate[list2.size()];
                    Predicate predicateOr = cb.or(list2.toArray(predicateO));
                    return query.where(predicateAnd, predicateOr).getRestriction();
                } else {
                    return predicateAnd;
                }
            }
        }, pageable);
    }

    public List<BreedDetail> findByBatch(String batch, boolean isout) {
        Breed breed = breedDao.findByBatch(batch);
        List<BreedDetail> result = null;
        if (breed == null) {
            throw new BusinessException("500", "批次号不存在");
        }
        if (breed.getSource().equals("繁殖")) {
            result = breedDetailDao.findByRelevanceIdAndSource(breed.getId(), "繁殖");
        } else {
            result = breedDetailDao.findByBatch(batch);
        }
        if (result != null && result.size() > 0 && isout) {
            Iterator<BreedDetail> it = result.iterator();
            while (it.hasNext()) {
                BreedDetail item = it.next();
                if (item.getIsOut() == 1) {
                    it.remove();
                }
            }
        }
        return result;
    }

    public List<BreedDetail> findByRfids(List<String> rfids) {
        return breedDetailDao.findByTypeAndCodeList("rfid", rfids);
    }
}
